# -*- coding: utf-8 -*-

import subprocess
import configparser
# import ConfigParser
import time

"""
A ssh based command dispatch system
"""

start = time.time()


def read_config(file="config.ini"):
    """Extract IP addresses and CMDS from config file and returns tuple"""
    ips = []
    cmds = []
    Config = configparser.ConfigParser()
    Config.read(file)
    machines = Config.items("MACHINES")
    commands = Config.items("COMMANDS")
    print(machines)
    print(commands)
    for ip in machines:
        ips.append(ip[1])
    for cmd in commands:
        cmds.append(cmd[1])
    return ips, cmds


ips, cmds = read_config()

# For every ip address, run all commands
for ip in ips:
    for cmd in cmds:
        subprocess.call("ssh lavenliu@%s %s" % (ip, cmd), shell=True)

end = time.time()
print("Dispatch Completed in {} seconds".format(float(end - start)))
